package com.metro.demo.excal;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.metro.core.vo.QueryParams;
import com.metro.demo.excal.beans.vo.ExcalTest;
import com.metro.demo.excal.mapper.IExcalTestMapper;
import com.metro.demo.excal.utils.ExcelUtil;
import org.apache.shiro.util.ThreadContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)  //使用junit4进行测试
@ContextConfiguration({"/application/applicationContext.xml"}) //加载配置文件
public class TextMain {


    @Resource
    org.apache.shiro.mgt.SecurityManager securityManager;

    @Before
    public void setUp() throws Exception {
        ThreadContext.bind(securityManager);

    }

    @Autowired
    private IExcalTestMapper excalMapper;
    @Test
    public void excalTest() throws Exception{
        //读取excel
        List<List<String>> readexecl = ExcelUtil.getExcelValues("D://客服问题信息.xls",1);
//        List<List<Map<String,String>>> readexeclC = excelTool.getExcelMapVal("D://物资明细.xls",1);
//        int count= excelTool.hasSheetCount("D://物资明细.xls");


        JSONArray json = new JSONArray();
        // 标题list 默认第一行为标题
        List<String> titleList = readexecl.get(0);
        for (int i = 1; i<readexecl.size(); i++){
            JSONObject jo = new JSONObject();
            for (int j = 0; j<readexecl.get(i).size();j++){
                jo.put(titleList.get(j),readexecl.get(i).get(j));
            }
            json.add(jo);
        }
        System.out.println(json.toString());

        ExcalTest entity = new ExcalTest();
        for (int i =0; i < json.size(); i++){
            JSONObject jo = json.getJSONObject(i);
            entity.setName((String) jo.get("经销商名称"));
            entity.setId(System.currentTimeMillis());
            entity.setModifyUserId(123L);
            entity.setCreateUserId(234L);
            entity.setMobile((String) jo.get("问题类型"));
            entity.setContent(jo.toString());
            excalMapper.save(entity);
        }
    }


    @Test
    public void queryJson(){

        List<ExcalTest> list = excalMapper.queryForList(new QueryParams<>());

        for (ExcalTest excal : list){
            JSONObject text = (JSONObject) JSONObject.parse(excal.getContent());
            for (String s : text.keySet()){
                System.out.println("key为： "+s);
                System.out.println("value为： "+ text.get(s));
                System.out.println("---------------------------");
            }
            System.out.println("*********************************************************************");
        }
        System.out.println(list.toString());

    }
}
